package com.servlet.resp; /**
 * @author wzm
 * @create 2024-01-27 16:13
 * project_name:JavaWeb2401
 * @Description 响应字节数据
 */

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/resp4")
public class ResponseServlet4 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //响应一个图片文件到浏览器  通过工具来读取输入流使用输出流

        //1.读取文件输入流
        FileInputStream fis = new FileInputStream("d://a.jpeg");//8848


        //2.获取response字节输出流对象
        ServletOutputStream os = response.getOutputStream();

        //3.输出 从输入流中读取写到输出流中  将输入流中的字节数据复制到输出流就可以输出了
        //使用IOUtils工具完成流的复制
        IOUtils.copy(fis,os);

        //4.关闭资源
        fis.close();


    }
}
